<?php
require_once("db_common.php");
require_once("Account.php");
require_once("Turnover.php");
require_once("Category.php");
function AddAccount($name,$money)
{
    $sql = "insert into account(account_name,account_money) values ('$name','$money')";
    
    if(mysql_query($sql))
    {
        return mysql_insert_id();
    } else 
    {
        output_mysqlerror();
        return -1;
    }
}
function AddCategory($type,$name,$img_id)
{
    $sql = "insert into category(category_type,category_name,category_img_id) values ('$type','$name','$img_id')";
    
    if(mysql_query($sql))
    {
        return mysql_insert_id();
    } else 
    {
        output_mysqlerror();
        return -1;
    }
}
function AddTurnover($paramArray)
{
    $sql = "insert into turnover(turnover_remark,turnover_from,turnover_to,
                                turnover_time,turnover_money,turnover_type,category_id)
                                values ('$paramArray[remark]','$paramArray[from]','$paramArray[to]',
                                '$paramArray[time]','$paramArray[money]','$paramArray[type]','$paramArray[catid]')";
    
    if(mysql_query($sql))
    {
        return mysql_insert_id();
    } else 
    {
        output_mysqlerror();
        return -1;
    }
}
function DeleteTurnover($id)
{
    $sql  = "delete from turnover where turnover_id = '$id'";
    if(mysql_query($sql))
    {
        return;
    } else 
    {
        output_mysqlerror();
        return -1;
    }
}
function GetIDsFromTable($tableName,$idName,$orderName)
{
 
    if($orderName)
    {
        $sql = "select $idName from $tableName ORDER BY $orderName DESC";
    }    else 
    {
        $sql = "select $idName from $tableName";
    }
    
    $idArray = array();
    if($query = mysql_query($sql))
    {
        while($row = mysql_fetch_array($query))
        {
            array_push($idArray,$row[$idName]);
        }
    } else 
    {
           echo "Get $idName from $tableName failed ";
           output_mysqlerror();
    }
    return $idArray;
}
function GetAccountIDs()
{
    return GetIDsFromTable("account","account_id","account_money");
}
function GetTurnoverIDs()
{
    return GetIDsFromTable("turnover","turnover_id","turnover_time"); 
}
function GetCategoryIDs()
{
    return GetIDsFromTable("category","category_id",""); 
}
function AppendsAccountsXMLNodes($doc)
{
     $idArray = GetAccountIDs();
     
     $r = $doc->createElement("accounts");
     $doc->appendChild($r);
  
     foreach($idArray as $id)
     {
        $acc = new Account;
        $acc->Init($id);
        $node = $acc->GetXMLNode($doc);
        $r->appendChild($node);
     }
}
function AppendsTurnoversXMLNodes($doc)
{
     $idArray = GetTurnoverIDs();
     
     $r = $doc->createElement("turnovers");
     $doc->appendChild($r);
  
     foreach($idArray as $id)
     {
        $acc = new Turnover;
        $acc->Init($id);
        $node = $acc->GetXMLNode($doc);
        $r->appendChild($node);
     }
}
function AppendsCategorysXMLNodes($doc)
{
     $idArray = GetCategoryIDs();
     
     $r = $doc->createElement("categorys");
     $doc->appendChild($r);
  
     foreach($idArray as $id)
     {
        $acc = new Category;
        $acc->Init($id);
        $node = $acc->GetXMLNode($doc);
        $r->appendChild($node);
     }
}

?>

